
import { _decorator, Event, instantiate, Node, Prefab, UITransform } from 'cc';
import { RenderManager } from '../Base/RenderManager';
import { SCENE_ENUM, Trigger_Status_Enum } from '../Enum';
import DataManager from '../Runtime/DataManager';
import { CircleManager } from './CircleManager';
const { ccclass, property } = _decorator;

const CIRCLE_RADIUS = 80;

@ccclass('H2AGameManager')
export class H2SceneManager extends RenderManager {
    @property([CircleManager])
    circles: CircleManager[] = [];

    @property(Prefab)
    linePrefab: Prefab = null;

    @property(Node)
    lines: Node = null;

    @property([Prefab])
    contents: Prefab[] = [];

    private circlesMap: Map<CircleManager, CircleManager[]> = new Map();
    start() {
        super.start();
        this.generateCirclesMap();
        this.generateLines();
        this.checkSuccess();
    }

    render(): void {
        this.renderContens();
    }

    renderContens() {
        for (let i = 0; i < this.circles.length; i++) {
            const circle = this.circles[i];
            circle.node.destroyAllChildren();

            const contentIdx = DataManager.Instance.H2AData[i];
            if (contentIdx !== null && this.contents[contentIdx]) {
                const content = instantiate(this.contents[contentIdx]);
                circle.node.addChild(content);
            }
        }
    }

    handleCircleTouch(e: Event, _index: string) {
        const index = parseInt(_index);
        if (DataManager.Instance.doorStatus === Trigger_Status_Enum.Resolved) {
            return;
        }

        const contentIdx = DataManager.Instance.H2AData[index];
        if (contentIdx === null) {
            return;
        }
        const curCircle = this.circles[index];
        const circles = this.circlesMap.get(curCircle);
        for (let i = 0; i < circles.length; i++) {
            const nextCircle = circles[i];

            const nullCircleIdx = DataManager.Instance.H2AData.findIndex(i => i === null);
            // const circleIdx = this.circles.findIndex(i => i === nextCircle);
            const circleIdx = nextCircle.index;
            if (nullCircleIdx === circleIdx) {
                DataManager.Instance.H2AData[nextCircle.index] = contentIdx;
                DataManager.Instance.H2AData[index] = null;
                DataManager.Instance.H2AData = [...DataManager.Instance.H2AData];
            }
        }
        this.checkSuccess();
    }

    checkSuccess() {
        if (DataManager.Instance.H2AData.every((e, i) => DataManager.Instance._H2AAnswer[i] === e)) {
            DataManager.Instance.doorStatus = Trigger_Status_Enum.Resolved;
            DataManager.Instance.curScene = SCENE_ENUM.H2;
        }
    }

    resetContents() {
        DataManager.Instance.H2AData = [...DataManager.Instance._H2AInitData];
    }

    generateCirclesMap() {
        this.circlesMap.set(this.circles[0], [this.circles[1], this.circles[4], this.circles[6]]);
        this.circlesMap.set(this.circles[1], [this.circles[0], this.circles[5], this.circles[6]]);
        this.circlesMap.set(this.circles[2], [this.circles[4], this.circles[6]]);
        this.circlesMap.set(this.circles[3], [this.circles[5], this.circles[6]]);
        this.circlesMap.set(this.circles[4], [this.circles[0], this.circles[2], this.circles[5], this.circles[6]]);
        this.circlesMap.set(this.circles[5], [this.circles[1], this.circles[3], this.circles[4], this.circles[6]]);
        this.circlesMap.set(this.circles[6], [this.circles[0], this.circles[1], this.circles[2], this.circles[3], this.circles[4], this.circles[5]]);

        for (let index = 0; index < this.circles.length; index++) {
            const circle = this.circles[index];
            circle.index = index;
        }
    }

    generateLines() {
        for (const [curCircle, circles] of this.circlesMap) {
            for (const nextCircle of circles) {
                const curIndex = this.circles.findIndex(i => i === curCircle);
                const nextIndex = this.circles.findIndex(i => i === nextCircle);
                if (curIndex < nextIndex) {
                    this.generateLine(curCircle, nextCircle)
                }
            }
        }
    }
    generateLine(curCircle: CircleManager, nextCircle: CircleManager) {
        const line = instantiate(this.linePrefab);

        const { x: x1, y: y1 } = curCircle.node.position;
        const { x: x2, y: y2 } = nextCircle.node.position;

        const x = (x1 + x2) / 2;
        const y = (y1 + y2) / 2;
        line.setPosition(x, y);

        const side1 = Math.abs(x1 - x2);
        const side2 = Math.abs(y1 - y2);
        const side3 = Math.sqrt(side1 ** 2 + side2 ** 2);
        const lineTransform = line.getComponent(UITransform);
        lineTransform.setContentSize(side3 - CIRCLE_RADIUS * 2, lineTransform.contentSize.height);

        const rad = Math.atan(side2 / side1);
        const angle = rad * 180 / Math.PI;

        const sign = (x1 > x2 && y1 > y2) || (x1 > x2 && y1 < y2) ? 1 : -1;

        line.setRotationFromEuler(0, 0, sign * angle);

        this.lines.addChild(line);
    }
}